#!/bin/bash

if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "./LTR_retriever-2.9.0.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

if [[ ! "$1" =~ ^/.* || ! -d "$1" || "$1" = "/" ]]; then echo "请输入正确的构建路径"; exit 1; fi
if [[ ! "$2" =~ ^/.* || ! -d "$2" || "$2" = "/" ]]; then echo "请输入正确的安装路径"; exit 1; fi
[[ "$1" = "$2" ]] && echo "构建路径与安装路径不能相同" && exit 1
buildpath=$1
installpath=$2
if [[ "$1" =~ .*/$ ]]; then buildpath=${buildpath%/*}; fi
if [[ "$2" =~ .*/$ ]]; then installpath=${installpath%/*}; fi

wget -t 10 -c -P $installpath https://github.com/oushujun/LTR_retriever/archive/v2.9.0.tar.gz -O $installpath/LTR_retriever-2.9.0.tar.gz
cd $installpath
tar xf LTR_retriever-2.9.0.tar.gz
mv LTR_retriever-2.9.0 LTR_retriever \
&& cd LTR_retriever \
&& sed -i \
    -e "s#BLAST+=#BLAST+=$installpath/rmblast/bin#" \
    -e "s#RepeatMasker=#RepeatMasker=$installpath/RepeatMasker#" \
    -e "s#LTR_retriever=#LTR_retriever=$installpath/LTR_retriever/bin#" \
    -e "s#CDHIT=#CDHIT=$installpath/cd-hit#" \
    paths
rm -rf $installpath/LTR_retriever-2.9.0.tar.gz
cat>"$installpath/LTR_retriever/LTR_retriever_modulefiles"<<EOF
#%Module1.0
conflict LTR_retriever
variable modfile [file normalize [info script]]
proc getModulefileDir {} {
    variable modfile
    set modfile_path [file dirname \$modfile]
    return \$modfile_path
}
set pwd [getModulefileDir]
set LTR_retriever \$pwd
setenv LTR_retriever \$LTR_retriever
prepend-path PATH \$LTR_retriever
EOF
